/**
 * 系统管理  角色管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>员工管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="formInline.username"
          placeholder="输入员工姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="formInline.sex"
          placeholder="输入员工性别"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEditAdd()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="55">
      </el-table-column>
      <el-table-column
        align="center"
        prop="username"
        label="员工姓名"
        width="110"
      >
      </el-table-column>
      <el-table-column
        align="center"
        prop="password"
        label="员工密码"
        width="300"
      >
      </el-table-column>
      <el-table-column align="center" prop="sex" label="员工性别">
      </el-table-column>
      <el-table-column align="center" prop="age" label="员工年龄" width="200">
      </el-table-column>
      <el-table-column align="center" prop="photo" label="员工头像" width="200">
        <template slot-scope="scope">
          <img :src="scope.row.photo" alt="" width="50" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <Pagination @callFather="callFather" :childMsg="formInline"></Pagination>

    <!-- 添加员工信息 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="35%"
      @click="closeDialog('edit')"
    >
      <el-form
        label-width="120px"
        :model="editForm"
        ref="editForm"
        :rules="rules"
      >
        <el-form-item label="员工姓名" prop="straffName_">
          <el-input
            size="small"
            v-model="editForm.username"
            auto-complete="off"
            placeholder="请输入员工姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工性别" prop="staffSex_">
          <el-input
            size="small"
            v-model="editForm.sex"
            auto-complete="off"
            placeholder="请输入员工性别"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工年龄" prop="straffAge_">
          <el-input
            size="small"
            v-model="editForm.age"
            auto-complete="off"
            placeholder="请输入员工年龄"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工头像" prop="staffPhotos_">
          <el-upload
            action="/api/admin/common/upload"
            list-type="picture-card"
            name="file"
            :auto-upload="true"
            :headers="headers"
            @on-change="handleChange"
            :on-success="handleImageUrl"
          >
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{ file }">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url"
                alt=""
              />
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <i class="el-icon-zoom-in"></i>
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('edit')">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm(editForm)"
          >保存</el-button
        >
      </div>
    </el-dialog>

    <!-- 菜单权限 -->
  </div>
</template>

<script>
import {
  roleList,
  roleSave,
  roleDelete,
  rolePwd,
  RoleRightTree,
  RoleRightSave,
  fetchDialogImageUrl,
  staffUpdate,
} from "../../api/userMG";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      total: 20,
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      menuAccessshow: false,
      editFormVisible: false, //控制编辑页面显示与隐藏
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      staffStatus: "", //
      headers: {
        token: localStorage.getItem("logintoken"),
      },
      title: "添加",
      editForm: {
        username: "",
        age: "",
        sex: "",
        photo: "",
      },
      // rules 表单验证
      rules: {
        staffName: [
          { required: true, message: "请输入员工姓名", trigger: "blur" },
        ],
        staffSex: [
          { required: true, message: "请输入员工性别", trigger: "blur" },
        ],
        staffAge: [
          { required: true, message: "请输入员工年龄", trigger: "blur" },
        ],
      },
      formInline: {
        page: 1,
        pageSize: 10,
        total: 1000,
        token: localStorage.getItem("logintoken"),
      },
      // 删除
      seletedata: {
        ids: "",
        token: localStorage.getItem("logintoken"),
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 数据权限
      RoleRight: [],
      RoleRightProps: {
        children: "children",
        label: "name",
      },
      // 选中
      checkmenu: [],
      //参数role
      saveroleId: "",
      // 分页参数
      // pageparm: {
      //   currentPage: 1,
      //   pageSize: 10,
      //   total: 10,
      // },
    };
  },
  // 注册组件
  components: {
    Pagination,
  },
  /**
   * 数据发生改变
   */

  watch: {},

  /**
   * 创建完毕
   */

  created() {
    this.getdata(this.formInline);
  },

  /**
   * 里面的方法只有被调用才会执行
   */

  methods: {
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    handleChange(file, fileList) {
      console.log(file);
    },
    handleImageUrl(res, file) {
      console.log(file);
      this.dialogImageUrl = URL.createObjectURL(file.raw);
      console.log(this.dialogImageUrl);
      console.log(res.data);
      this.editForm.photo = res.data;
    },
    // 获取角色列表
    getdata(parameter) {
      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      roleList(parameter)
        .then((res) => {
          this.loading = false;
          if (res.success == false) {
            this.$message({
              type: "info",
              message: res.msg,
            });
          } else {
            this.listData = res.data.records;
            // 分页赋值
            // this.pageparm.currentPage = this.formInline.page;
            // this.pageparm.pageSize = this.formInline.limit;
            this.formInline.total = res.data.total * 1;
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error("获取员工列表失败，请稍后再试！");
        });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.pageSize = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline);
    },
    //显示编辑界面
    handleEdit: function (row) {
      this.staffStatus = "update";
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        // this.title = "编辑";
        // this.editForm.id = row.id;
        // this.editForm.username = row.username;
        // this.editForm.sex = row.sex;
        // this.editForm.age = row.age;
        // this.editForm.photo = row.dialogImageUrl;
        this.editForm = row;
      }
    },
    handleEditAdd: function (row) {
      this.editFormVisible = true;
      this.staffStatus = "add";
      if (row != undefined && row != "undefined") {
        this.title = "添加";
        this.editForm.username = row.username;
        this.editForm.sex = row.sex;
        this.editForm.age = row.age;
        this.editForm.photo = row.dialogImageUrl;
      }
    },
    async getImageUrl() {
      try {
        const data = {
          // 设置POST请求的数据
          url: this.dialogImageUrl,
        };
        // this.dialogImageUrl = await fetchDialogImageUrl(data);
        let res = await fetchDialogImageUrl(data);
        console.log(res);
      } catch (error) {
        console.error(error);
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          // console.log(valid);
          if (this.staffStatus == "add") {
            roleSave(this.editForm)
              .then((res) => {
                this.editFormVisible = false;
                this.loading = false;
                console.log(res);
                if (res.code == 1) {
                  this.getdata(this.formInline);
                  this.$message({
                    type: "success",
                    message: "员工保存成功！",
                  });
                } else {
                  this.$message({
                    type: "info",
                    message: res.msg,
                  });
                }
              })
              .catch((err) => {
                this.editFormVisible = false;
                this.loading = false;
                this.$message.error("员工保存失败，请稍后再试！");
              });
          } else {
            // 修改请求
            staffUpdate(this.editForm)
              .then((res) => {
                this.editFormVisible = false;
                this.loading = false;
                if (res.code == 1) {
                  this.getdata(this.formInline);
                  this.$message({
                    type: "success",
                    message: "员工保存成功！",
                  });
                } else {
                  this.$message({
                    type: "info",
                    message: res.msg,
                  });
                }
              })
              .catch((err) => {
                this.editFormVisible = false;
                this.loading = false;
                this.$message.error("员工保存失败，请稍后再试！");
              });
          }
        } else {
          return false;
        }
      });
    },

    // 删除角色
    deleteUser(row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          roleDelete(row.id)
            .then((res) => {
              if (res.code == 1) {
                this.$message({
                  type: "success",
                  message: "角色已删除！",
                });
                this.getdata(this.formInline);
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error("角色删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 数据权限

    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == "edit") {
        this.editFormVisible = false;
      } else if (dialog == "perm") {
        this.menuAccessshow = false;
      }
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 